//
//  TableController.m
//  TianyaGuest
//
//  Created by liuwenyang on 14-8-19.
//  Copyright (c) 2014年 liuwenyang. All rights reserved.
//

#import "TableController.h"
#import <objc/message.h>
#import <MJRefresh.h>
#import <ReactiveCocoa.h>
#define kPageSize 10
@interface TableController () <TableControllerDelegate>

@end

@implementation TableController

-(id)initWithFrame:(CGRect)frame
{
    self = [super init];
    if (self) {
        _tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
    }
    return self;
}

-(id)initWithAutoLayout
{
    self = [super init];
    if (self) {
        _tableView = [[UITableView alloc] init];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];

    }
    return self;
}


-(id)initWithClassForCell:(Class)classForCell
{
    self = [self initWithAutoLayout];
    if (self) {
        
        _classForCell = classForCell;
        UINib *cellNib = [UINib nibWithNibName:NSStringFromClass(_classForCell) bundle:nil];
        [_tableView registerNib:cellNib forCellReuseIdentifier:NSStringFromClass(_classForCell)];

    }
    return self;
}


- (NSArray *)tableDataSource
{
    return _listArray;
}

- (void)dealloc
{
    _delegate = nil;
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
    [_tableView removeFromSuperview];
    _tableView = nil;
    NSLog(@"TableController dealloc");
    _listArray = nil;
    _listTotal = nil;
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_listArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(_classForCell) forIndexPath:indexPath];
//    if (cell == nil) {
//        
//        cell = [[_classForCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NSStringFromClass(_classForCell)];
//        cell.delegate = self;
//    }
    cell.delegate = self;
    id data;
    if (self.listArray.count > 0) {
        data = [_listArray objectAtIndex:indexPath.row];
    }
    [cell setValue:data];
    [cell setValue:data andIndexPath:indexPath];
    
    return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.listArray.count == 0) {
        return 0.0;
    }
    id data = [_listArray objectAtIndex:indexPath.row];
    return [_classForCell heightForTableViewCell:data];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (_delegate && [_delegate respondsToSelector:@selector(didSelectedCell:tableViewCell:)]) {
        TableViewCell *tableViewCell = (TableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        [_delegate didSelectedCell:self tableViewCell:tableViewCell];
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(didSelectedCell:dataSrc:row:)]) {
        id data = [_listArray objectAtIndex:indexPath.row];
        [_delegate didSelectedCell:self dataSrc:data row:indexPath.row];
    }
}



#pragma tableCellDelegate

-(void)clickElement:(TableViewCell *)tableViewCell element:(id)element cellData:(id)cellData
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickCellSubView:subView:subViewData:)]) {
        [self.delegate didClickCellSubView:self subView:element subViewData:cellData];
    }
}

@end



@implementation RefreshTableController



-(id)initWithDragRefresh:(BOOL)enableDragRefresh withInfiniteScroll:(BOOL)enableInfiniteScroll
{
    self = [super initWithAutoLayout];
    if (self) {
        if (enableDragRefresh) {
            self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeaderViewDidCallBack)];
        }
        if (enableInfiniteScroll) {
            self.tableView.mj_footer  = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooterViewDidCallBack)];
        }
        _pageIndex = 1;
    }
    return self;
}

-(id)initWithDragRefresh:(BOOL)enableDragRefresh withInfiniteScroll:(BOOL)enableInfiniteScroll classForCell:(Class)classForCell
{
    self = [super initWithClassForCell:classForCell];
    if (self) {
        if (enableDragRefresh) {
            self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeaderViewDidCallBack)];
//            self.tableView.mj_header = [DSRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeaderViewDidCallBack)];
        }
        if (enableInfiniteScroll) {
            self.tableView.mj_footer  = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooterViewDidCallBack)];
        }
        _pageIndex = 1;
    }
    return self;
}

-(id)initWithDragRefresh:(BOOL)enableDragRefresh withInfiniteScroll:(BOOL)enableInfiniteScroll classForCell:(Class)classForCell refreshHeaderType:(NSInteger)refreshHeaderType
{
    self = [super initWithClassForCell:classForCell];
    if (self) {
        if (enableDragRefresh) {
                        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeaderViewDidCallBack)];
//            self.tableView.mj_header = [DSMoonRiseRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeaderViewDidCallBack)];
        }
        if (enableInfiniteScroll) {
            self.tableView.mj_footer  = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooterViewDidCallBack)];
        }
    }
    return self;
}

- (void)refreshTable:(void (^)(BOOL))block
{
    //virtual method
}

- (void)getTableMore:(void (^)(BOOL))block
{
    //virtual method
}

- (void)forceRefresh
{
    [self refreshHeaderViewDidCallBack];
}

-(void)forceRefreshWithoutAnimation
{
    @weakify(self);
    self.pageIndex = 1;
    [self refreshTable:^(BOOL finished) {
        @strongify(self);
        if (self.totalCount > self.listArray.count) {
            //需要展现加载更多
            self.tableView.mj_footer  = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooterViewDidCallBack)];
        }else{
            [self.tableView.mj_footer removeFromSuperview];
            self.tableView.tableFooterView = [[UIView alloc] init];
        }
    }];
}




#pragma mark - 顶部的重新刷新
#pragma mark RefreshHeaderViewDelegate

- (void)refreshHeaderViewDidCallBack
{
    [self.tableView.mj_header beginRefreshing];
    self.pageIndex = 1;
    __weak RefreshTableController  *weakSelf = self;
    [self refreshTable:^(BOOL finished) {
        if (finished) {
            [weakSelf.tableView.mj_header endRefreshing];
            
            if (weakSelf.totalCount > weakSelf.listArray.count) {
                //需要展现加载更多
                weakSelf.tableView.mj_footer  = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:weakSelf refreshingAction:@selector(refreshFooterViewDidCallBack)];
            }else{
                [weakSelf.tableView.mj_footer removeFromSuperview];
                weakSelf.tableView.tableFooterView = [[UIView alloc] init];
            }
            
            //通知代理model加载完成
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(modelDidLoadFinished:)]) {
                [weakSelf.delegate modelDidLoadFinished:self];
            }
        } else {
            [weakSelf.tableView.mj_header endRefreshing];
            NSLog(@"modelDidLoadFailure");
        }
    }];
}

#pragma mark - 底部的获取更多
#pragma mark RefreshFooterViewDelegate

- (void)refreshFooterViewDidCallBack
{
    [self.tableView.mj_footer beginRefreshing];
    __weak RefreshTableController *weakSelf = self;
    ++self.pageIndex;
    [self getTableMore:^(BOOL finished) {
        if (finished) {
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        if (weakSelf.totalCount > weakSelf.listArray.count) {
            //需要展现加载更多
            weakSelf.tableView.mj_footer  = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:weakSelf refreshingAction:@selector(refreshFooterViewDidCallBack)];
        }else{
            [weakSelf.tableView.mj_footer removeFromSuperview];
            weakSelf.tableView.tableFooterView = [[UIView alloc] init];
        }
//        pageIndex_ += 1;
        //通知代理model加载完成
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(modelDidLoadFinished:)]) {
            [weakSelf.delegate modelDidLoadFinished:self];
        }
    }];
}



@end





















